// pages/login/login.js
import { Home } from "../../models/home";
import { User } from "../../models/user";
import { UserInfo } from "../../models/userinfo";
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
import Base64 from "../../utils/base64";
import { config } from "../../config/config";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    errMsg1: "",
    errMsg2: "",
    Username: "",
    Password: "",
    userInfo: null,
    config
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.userInfo = new UserInfo();
  },

  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.data.Username = event.detail;
  },
  onBlur(event) {
    this.data.Username = event.detail.value;
  },
  onBlur2(event) {
    this.data.Password = event.detail.value;
  },

  onChange2(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.data.Password = event.detail;
  },

  decode(input) {
    // 解密
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
      enc1 = this._keyStr.indexOf(input.charAt(i++));
      enc2 = this._keyStr.indexOf(input.charAt(i++));
      enc3 = this._keyStr.indexOf(input.charAt(i++));
      enc4 = this._keyStr.indexOf(input.charAt(i++));
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      output = output + String.fromCharCode(chr1);
      if (enc3 != 64) {
        output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
        output = output + String.fromCharCode(chr3);
      }
    }
    return this._utf8to16(output);
  },

  async confirm() {
    if (this.valid()) {
      let postData = {
        Username: this.data.Username,
        Password: this.data.Password
      };
      const data = await Home.SubmitLogin(postData);
      if (data.Success) {
        Toast("登录成功");
        let token = data.Data;
        // let user = decodeURIComponent(escape(window.atob(token.split(".")[1])));
        let user = Base64.decode(token.split(".")[1]);
        console.log(user);
        user = user.replace(/"/g, "");

        let userId = user
          .split(",")[0]
          .split(":")[1]
          .replace(/"/g, "");
        console.log(userId);
        const data2 = await User.GetTheData({ id: userId });
        console.log(data2.Data);
        let userinfo = {
          token,
          ...data2.Data
        };
        console.log(this.data.userInfo);
        this.data.userInfo.setUserInfo(userinfo);
        wx.nextTick(() => {
          wx.reLaunch({
            //跳转到tabBar页面，并关闭其他所有tabBar页面
            url: "/pages/me/me"
          });
        });

        // this.$router.push({
        //   path: "/"
        // })
      } else {
        Toast(data.Msg);
      }
    }
  },

  goReg() {
    wx.navigateTo({
      url: `/pages/identity-selection/identity-selection`
    });
  },

  valid1() {
    let val = true;
    if (!this.data.Username) {
      val = false;
      this.setData({
        errMsg1: "用户名不能为空"
      });
    } else {
      this.setData({
        errMsg1: ""
      });
    }

    return val;
  },
  valid2() {
    let val = true;
    if (!this.data.Password) {
      val = false;
      this.setData({
        errMsg2: "密码不能为空"
      });
    } else {
      this.setData({
        errMsg2: ""
      });
    }
    return val;
  },
  valid() {
    let val1 = this.valid1();
    let val3 = this.valid2();
    return val1 && val3;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
